package com.tmc.service.intcar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intcar.EthUpdateOrderReq;
import com.tmc.bean.intcar.EthUpdateOrderResp;
import com.tmc.entity.intcar.IntcarOrder;
import com.tmc.entity.intcar.IntcarPassenger;
import com.tmc.util.ObjectUtil;
import com.tmc.vendor.intcar.common.ServiceCenter;
import com.tmc.vendor.intcar.common.VendorService;

/**
 * @author zhang
 * @date 2017年12月23日 上午9:45:53 
 */
@Service
public class IntcarUpdateOrderServiceImpl {
	
	private Log log = LogFactory.getLog(IntcarUpdateOrderServiceImpl.class);

	
	@Autowired 
	private IntcarOrderServiceImpl orderService;
	
	public CommonResult updating(ContextInfo context, EthUpdateOrderReq req){
		IntcarOrder dbOrder = orderService.getById(context, req.getOrderID());
		if(dbOrder == null){
			return CommonResult.makeFail(req.getOrderID()+"的订单不存在！");
		}
		VendorService service = ServiceCenter.getService(dbOrder.getVendor());
		try {
			//更新供应商订单
			EthUpdateOrderResp resp = service.updateOrder(context, req);
			if(!resp.isSuccess()){
				return CommonResult.makeFail(resp.getMessage());
			}
			//更新本地订单
			IntcarOrder order = buildIntcarOrder(context, req);
			orderService.updatePassenger(context, order);
			return CommonResult.makeSuccess("订单更新成功！");
		} catch (Exception e) {
			log.error("订单更新失败！", e);
		}
		return CommonResult.makeFail("订单更新失败！");
		
	}
	
	private IntcarOrder buildIntcarOrder(ContextInfo context, EthUpdateOrderReq req){
		IntcarOrder order = new IntcarOrder();
		order.setOrderID(req.getOrderID());
		order.setPassengerList(ObjectUtil.makeTargetList(req.getPassengers(), IntcarPassenger.class));
		return order;
	}
}
